package com.github.baichuan.application.dashboard.config;

import com.github.baichuan.web.valication.ValidationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.support.WebExchangeBindException;

import java.util.Map;

@RestControllerAdvice
public class GlobalError {
    @ExceptionHandler(WebExchangeBindException.class)
    public ResponseEntity handleValidationErrors(WebExchangeBindException ex){
        BindingResult result = ex.getBindingResult();
        Map<String, String> errors = ValidationUtils.message(result);
        return new ResponseEntity(errors, HttpStatus.BAD_REQUEST);
    }
}
